var model = require('../model');

var Worker = model.Worker;
var Position = model.Position;

//# 员工信息
exports.index = function (req, res, next) {
    var promise = Position.find({ deleted: false }).exec();
    promise.then(function (positions) {
        res.render('backend/worker/index', {
            title: '员工信息',
            positions: positions
        });
    }, function (error) {
        console.log(error);
    });
};


//# 员工列表
exports.list = function (req, res, next) {
    var pageInfo = {};

    pageInfo.pageIndex = parseInt(req.query.pageIndex) || 1;
    pageInfo.pageSize = parseInt(req.query.pageSize) || 10;
    var skipIndex = (pageInfo.pageIndex - 1) * (pageInfo.pageSize);

    var nameReg = new RegExp(req.query.name);
    var positionId = req.query.positionId;

    var query = function () {
        if (positionId) {
            return {
                deleted: false,
                name: nameReg,
                positionId: positionId
            }
        } else {
            return {
                deleted: false,
                name: nameReg
            }
        }
    } ();


    Worker.count(query, function (error, count) {
        if (error) {
            console.log(error);
        } else {
            pageInfo.count = count;
            pageInfo.totalPage = Math.ceil(count / pageInfo.pageSize);
        }
    });

    Worker.find(query)
        .skip(skipIndex)
        .limit(pageInfo.pageSize)
        .exec(function (error, workers) {
            if (error) {
                console.log(error);
                return;
            }
            res.render('backend/worker/list', {
                workers: workers,
                pageInfo: pageInfo
            });
        });
};

//# 录入员工信息
exports.new = function (req, res, next) {
    var promise = Position.find({ deleted: false }).exec();
    promise.then(function (positions) {
        res.render('backend/worker/new', {
            title: '添加职位',
            positions: positions
        });
    }, function (error) {
        console.log(error);
    });

};

//# 保存新员工
exports.create = function (req, res, next) {
    var name = req.body.name;
    var positionId = req.body.positionId;
    var remark = req.body.remark;
    var worker = new Worker({
        name: name,
        positionId: positionId,
        remark: remark,
        deleted: false
    });
    worker.save(function (error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.append('Location', '/backend/workers');
            res.json(result);
        }
    });
};

//# 编辑员工界面
exports.edit = function (req, res, next) {
    var workerId = req.params.id;
    Position.find({ deleted: false }, function (error, positions) {
        if (error) {
            console.log(error);
        } else {
            Worker.findOne({ _id: workerId }, function (error, worker) {
                if (error) {
                    console.log(error);
                } else {
                    res.render('backend/worker/edit', {
                        title: '编辑职位',
                        positions: positions,
                        worker: worker
                    });
                }
            });
        }
    });
};

//# 更新员工信息
exports.update = function (req, res, next) {
    var name = req.body.name;
    var positionId = req.body.positionId;
    var remark = req.body.remark;
    var workerId = req.params.id;

    var update = {
        name: name,
        remark: remark,
        positionId: positionId
    };

    Worker.update({ _id: workerId }, update, function (error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};

//# 删除员工信息
exports.delete = function (req, res, next) {
    var workerId = req.params.id;

    Worker.update({ _id: workerId }, { deleted: true }, function (error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};
